{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using VMLS\n",
    "using LinearAlgebra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chapter 12\n",
    "# Least squares\n",
    "### 12.1 Least squares problem\n",
    "We take the small least squares problem of Figure [12.1](https://web.stanford.edu/~boyd/vmls/vmls.pdf#figure.12.1) in VMLS and check that $‖Ax̂− b‖$ is less than $‖Ax− b‖$ for some other value of $x$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " -0.33333333333333337\n",
       " -0.6666666666666666 \n",
       "  0.33333333333333337"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [ 2 0 ; -1 1 ; 0 2 ]\n",
    "b = [ 1, 0, -1 ]\n",
    "xhat = [ 1/3, -1/3 ]\n",
    "rhat = A*xhat -b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.816496580927726"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm(rhat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       "  0.0\n",
       " -1.0\n",
       "  0.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [ 1/2, -1/2 ]\n",
    "r = A*x -b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12.2 Solution\n",
    "**Least squares solution formula.** Let’s check the solution formulas ([12.5](https://web.stanford.edu/~boyd/vmls/vmls.pdf#equation.12.2.5) and\n",
    "[12.6](https://web.stanford.edu/~boyd/vmls/vmls.pdf#equation.12.2.6)) in VMLS, $x̂ = (A^TA)^{−1}A^T b = A^†b$ for the small example of Figure [12.1](https://web.stanford.edu/~boyd/vmls/vmls.pdf#figure.12.1)) (where $x̂ = (\\frac{1}{3},\\frac{1}{3})$)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Float64,1}:\n",
       "  0.33333333333333337\n",
       " -0.33333333333333337"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inv(A'*A)*A'*b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Float64,1}:\n",
       "  0.3333333333333333\n",
       " -0.3333333333333335"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pinv(A)*b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Float64,1}:\n",
       " -2.220446049250313e-16\n",
       "  2.220446049250313e-16"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(A'*A)*xhat - A'*b # Check that normal equations hold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Orthogonality principle.** Let’s check the orthogonality principle ([12.9](https://web.stanford.edu/~boyd/vmls/vmls.pdf#equation.12.2.9)), for the same example. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.220446049250313e-16"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = [-1.1, 2.3];\n",
    "(A*z)'*rhat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-6.661338147750939e-16"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = [5.3, -1.2];\n",
    "(A*z)'*rhat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12.3 Solving least squares problems\n",
    "Julia uses the backslash operator to denote the least squares approximate solution: `xhat = A\\b`. (The same operator is used to solve square systems of linear equations, and we will see more uses of it in later chapters.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2.393918396847994e-16, 7.140067880219723e-16, 7.694823502053512e-16)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = randn(100,20); b = randn(100);\n",
    "x1 = A\\b; # Least squares using backslash operator\n",
    "x2 = inv(A'*A)*(A'*b); # Using formula\n",
    "x3 = pinv(A)*b; # Using pseudo-inverse\n",
    "Q, R = qr(A);\n",
    "Q = Matrix(Q);\n",
    "x4 = R\\(Q'*b); # Using QR factorization\n",
    "norm(x1-x2),norm(x2-x3),norm(x3-x4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Complexity.** The complexity of solving the least squares problem with $m × n$\n",
    "matrix $A$ is around $2mn^2$ flops. Let’s check this in Julia by solving a few least\n",
    "squares problems of different dimensions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  0.054153 seconds (4.05 k allocations: 12.030 MiB, 7.22% gc time)\n",
      "  0.055149 seconds (4.05 k allocations: 12.030 MiB, 22.64% gc time)\n",
      "  0.176924 seconds (4.05 k allocations: 19.675 MiB, 25.20% gc time)\n",
      "  0.228731 seconds (8.05 k allocations: 31.607 MiB, 1.73% gc time)\n"
     ]
    }
   ],
   "source": [
    "m = 2000; n = 500;\n",
    "A = randn(m,n); b = randn(m);\n",
    "@time x = A\\b;\n",
    "@time x = A\\b;\n",
    "m = 4000; n = 500;\n",
    "A = randn(m,n); b = randn(m);\n",
    "@time x = A\\b;\n",
    "m = 2000; n = 1000;\n",
    "A = randn(m,n); b = randn(m);\n",
    "@time x = A\\b;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that doubling $m$ approximately doubles the computation time, and\n",
    "doubling $n$ increases it by around a factor of four. The times above can be used to\n",
    "guess the speed of the computer on which it was carried out. For example, using\n",
    "the last problem solved, the number of flops is around $2mn2 = 4 · 10^9$, and it took\n",
    "around $0.4$ seconds. This suggests a speed of around $10 Gflop/sec$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Matrix least squares.** Let’s solve multiple least squares problems with the same\n",
    "matrix $A$ and different vector $b$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = randn(1000,100); B = randn(1000,10);\n",
    "X = A\\B;\n",
    "# Check that third column of X is least squares solution\n",
    "# with third column of B\n",
    "x3 = A\\B[:,3];\n",
    "norm(X[:,3]-x3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 12.4 Examples\n",
    "**Advertising purchases.** We work out the solution of the optimal advertising purchase problem on page [234](https://web.stanford.edu/~boyd/vmls/vmls.pdf#section*.284) of VMLS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       "   62.07662454385102\n",
       "   99.98500402826197\n",
       " 1442.8374625412223 "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R = [ 0.97 1.86 0.41;\n",
    "1.23 2.18 0.53;\n",
    "0.80 1.24 0.62;\n",
    "1.29 0.98 0.51;\n",
    "1.10 1.23 0.69;\n",
    "0.67 0.34 0.54;\n",
    "0.87 0.26 0.62;\n",
    "1.10 0.16 0.48;\n",
    "1.92 0.22 0.71;\n",
    "1.29 0.12 0.62];\n",
    "m, n = size(R);\n",
    "vdes = 1e3 * ones(m);\n",
    "s = R \\ vdes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "132.63819026326524"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rms(R*s - vdes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Illumination.** The following code constructs and solves the illumination problem on page [234](https://web.stanford.edu/~boyd/vmls/vmls.pdf#section*.285), and plots two histograms with the pixel intensity distributions (Figure 12.1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1403904813427606"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 10; # number of lamps\n",
    "lamps = [ # x, y positions of lamps and height above floor\n",
    "    4.1 20.4 4;\n",
    "    14.1 21.3 3.5;\n",
    "    22.6 17.1 6;\n",
    "    5.5 12.3 4.0;\n",
    "    12.2 9.7 4.0;\n",
    "    15.3 13.8 6;\n",
    "    21.3 10.5 5.5;\n",
    "    3.9 3.3 5.0;\n",
    "    13.1 4.3 5.0;\n",
    "    20.3 4.2 4.5 ];\n",
    "N = 25; # grid size\n",
    "m = N*N; # number of pixels\n",
    "# construct m x 2 matrix with coordinates of pixel centers\n",
    "pixels = hcat( \n",
    "    reshape( collect(0.5: 1 : N) * ones(1,N), m, 1), \n",
    "    reshape( ones(N,1) * collect(0.5: 1 : N)', m, 1));\n",
    "# The m x n matrix A maps lamp powers to pixel intensities.\n",
    "# A[i,j] is inversely proportional to the squared distance of\n",
    "# lamp j to pixel i.\n",
    "A = zeros(m,n);\n",
    "for i=1:m\n",
    "    for j=1:n\n",
    "        A[i,j] = 1.0 / norm([pixels[i,:]; 0] - lamps[j,:])^2;\n",
    "        end;\n",
    "    end;\n",
    "A = (m/sum(A)) * A; # scale elements of A\n",
    "# Least squares solution\n",
    "x = A \\ ones(m,1);\n",
    "rms_ls = rms(A*x .- 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip2700\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip2700)\" d=\"\n",
       "M0 1600 L2400 1600 L2400 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip2701\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip2700)\" d=\"\n",
       "M167.279 1487.47 L2352.76 1487.47 L2352.76 47.2441 L167.279 47.2441  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip2702\">\n",
       "    <rect x=\"167\" y=\"47\" width=\"2186\" height=\"1441\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  481.991,1487.47 481.991,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  871.004,1487.47 871.004,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1260.02,1487.47 1260.02,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1649.03,1487.47 1649.03,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2038.04,1487.47 2038.04,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,1487.47 2352.76,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,1247.44 2352.76,1247.44 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,1007.4 2352.76,1007.4 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,767.359 2352.76,767.359 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,527.321 2352.76,527.321 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,287.283 2352.76,287.283 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,47.2441 2352.76,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1487.47 2352.76,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1487.47 167.279,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  481.991,1487.47 481.991,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  871.004,1487.47 871.004,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1260.02,1487.47 1260.02,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1649.03,1487.47 1649.03,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2038.04,1487.47 2038.04,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1487.47 200.061,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1247.44 200.061,1247.44 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1007.4 200.061,1007.4 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,767.359 200.061,767.359 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,527.321 200.061,527.321 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,287.283 200.061,287.283 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip2700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,47.2441 200.061,47.2441 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 481.991, 1541.47)\" x=\"481.991\" y=\"1541.47\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 871.004, 1541.47)\" x=\"871.004\" y=\"1541.47\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1260.02, 1541.47)\" x=\"1260.02\" y=\"1541.47\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1649.03, 1541.47)\" x=\"1649.03\" y=\"1541.47\">1.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2038.04, 1541.47)\" x=\"2038.04\" y=\"1541.47\">1.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 1504.97)\" x=\"143.279\" y=\"1504.97\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 1264.94)\" x=\"143.279\" y=\"1264.94\">20</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 1024.9)\" x=\"143.279\" y=\"1024.9\">40</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 784.859)\" x=\"143.279\" y=\"784.859\">60</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 544.821)\" x=\"143.279\" y=\"544.821\">80</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 304.783)\" x=\"143.279\" y=\"304.783\">100</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip2700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 64.7441)\" x=\"143.279\" y=\"64.7441\">120</text>\n",
       "</g>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M287.484 1487.47 L287.484 1487.47 L365.287 1487.47 L365.287 1487.47 L287.484 1487.47 L287.484 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  287.484,1487.47 287.484,1487.47 365.287,1487.47 287.484,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M365.287 1487.47 L365.287 1487.47 L443.09 1487.47 L443.09 1487.47 L365.287 1487.47 L365.287 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  365.287,1487.47 365.287,1487.47 443.09,1487.47 365.287,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M443.09 1487.47 L443.09 1487.47 L520.892 1487.47 L520.892 1487.47 L443.09 1487.47 L443.09 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  443.09,1487.47 443.09,1487.47 520.892,1487.47 443.09,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M520.892 1439.47 L520.892 1487.47 L598.695 1487.47 L598.695 1439.47 L520.892 1439.47 L520.892 1439.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  520.892,1439.47 520.892,1487.47 598.695,1487.47 598.695,1439.47 520.892,1439.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M598.695 1439.47 L598.695 1487.47 L676.498 1487.47 L676.498 1439.47 L598.695 1439.47 L598.695 1439.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  598.695,1439.47 598.695,1487.47 676.498,1487.47 676.498,1439.47 598.695,1439.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M676.498 1403.46 L676.498 1487.47 L754.3 1487.47 L754.3 1403.46 L676.498 1403.46 L676.498 1403.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  676.498,1403.46 676.498,1487.47 754.3,1487.47 754.3,1403.46 676.498,1403.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M754.3 1223.43 L754.3 1487.47 L832.103 1487.47 L832.103 1223.43 L754.3 1223.43 L754.3 1223.43  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  754.3,1223.43 754.3,1487.47 832.103,1487.47 832.103,1223.43 754.3,1223.43 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M832.103 1271.44 L832.103 1487.47 L909.906 1487.47 L909.906 1271.44 L832.103 1271.44 L832.103 1271.44  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  832.103,1271.44 832.103,1487.47 909.906,1487.47 909.906,1271.44 832.103,1271.44 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M909.906 911.382 L909.906 1487.47 L987.708 1487.47 L987.708 911.382 L909.906 911.382 L909.906 911.382  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  909.906,911.382 909.906,1487.47 987.708,1487.47 987.708,911.382 909.906,911.382 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M987.708 1187.43 L987.708 1487.47 L1065.51 1487.47 L1065.51 1187.43 L987.708 1187.43 L987.708 1187.43  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  987.708,1187.43 987.708,1487.47 1065.51,1487.47 1065.51,1187.43 987.708,1187.43 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1065.51 971.392 L1065.51 1487.47 L1143.31 1487.47 L1143.31 971.392 L1065.51 971.392 L1065.51 971.392  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1065.51,971.392 1065.51,1487.47 1143.31,1487.47 1143.31,971.392 1065.51,971.392 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1143.31 383.298 L1143.31 1487.47 L1221.12 1487.47 L1221.12 383.298 L1143.31 383.298 L1143.31 383.298  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1143.31,383.298 1143.31,1487.47 1221.12,1487.47 1221.12,383.298 1143.31,383.298 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1221.12 143.259 L1221.12 1487.47 L1298.92 1487.47 L1298.92 143.259 L1221.12 143.259 L1221.12 143.259  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1221.12,143.259 1221.12,1487.47 1298.92,1487.47 1298.92,143.259 1221.12,143.259 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1298.92 299.284 L1298.92 1487.47 L1376.72 1487.47 L1376.72 299.284 L1298.92 299.284 L1298.92 299.284  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1298.92,299.284 1298.92,1487.47 1376.72,1487.47 1376.72,299.284 1298.92,299.284 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1376.72 719.352 L1376.72 1487.47 L1454.52 1487.47 L1454.52 719.352 L1376.72 719.352 L1376.72 719.352  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1376.72,719.352 1376.72,1487.47 1454.52,1487.47 1454.52,719.352 1376.72,719.352 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1454.52 935.386 L1454.52 1487.47 L1532.33 1487.47 L1532.33 935.386 L1454.52 935.386 L1454.52 935.386  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1454.52,935.386 1454.52,1487.47 1532.33,1487.47 1532.33,935.386 1454.52,935.386 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1532.33 1163.42 L1532.33 1487.47 L1610.13 1487.47 L1610.13 1163.42 L1532.33 1163.42 L1532.33 1163.42  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1532.33,1163.42 1532.33,1487.47 1610.13,1487.47 1610.13,1163.42 1532.33,1163.42 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1610.13 1367.46 L1610.13 1487.47 L1687.93 1487.47 L1687.93 1367.46 L1610.13 1367.46 L1610.13 1367.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1610.13,1367.46 1610.13,1487.47 1687.93,1487.47 1687.93,1367.46 1610.13,1367.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1687.93 1463.47 L1687.93 1487.47 L1765.73 1487.47 L1765.73 1463.47 L1687.93 1463.47 L1687.93 1463.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1687.93,1463.47 1687.93,1487.47 1765.73,1487.47 1765.73,1463.47 1687.93,1463.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1765.73 1463.47 L1765.73 1487.47 L1843.54 1487.47 L1843.54 1463.47 L1765.73 1463.47 L1765.73 1463.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1765.73,1463.47 1765.73,1487.47 1843.54,1487.47 1843.54,1463.47 1765.73,1463.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1843.54 1487.47 L1843.54 1487.47 L1921.34 1487.47 L1921.34 1487.47 L1843.54 1487.47 L1843.54 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1843.54,1487.47 1843.54,1487.47 1921.34,1487.47 1843.54,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1921.34 1487.47 L1921.34 1487.47 L1999.14 1487.47 L1999.14 1487.47 L1921.34 1487.47 L1921.34 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1921.34,1487.47 1921.34,1487.47 1999.14,1487.47 1921.34,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M1999.14 1487.47 L1999.14 1487.47 L2076.95 1487.47 L2076.95 1487.47 L1999.14 1487.47 L1999.14 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1999.14,1487.47 1999.14,1487.47 2076.95,1487.47 1999.14,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M2076.95 1487.47 L2076.95 1487.47 L2154.75 1487.47 L2154.75 1487.47 L2076.95 1487.47 L2076.95 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2076.95,1487.47 2076.95,1487.47 2154.75,1487.47 2076.95,1487.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip2702)\" d=\"\n",
       "M2154.75 1487.47 L2154.75 1487.47 L2232.55 1487.47 L2232.55 1487.47 L2154.75 1487.47 L2154.75 1487.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip2702)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2154.75,1487.47 2154.75,1487.47 2232.55,1487.47 2154.75,1487.47 \n",
       "  \"/>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Plots\n",
    "histogram(A*x, bins = (0.375:0.05:1.625), legend = false, ylim = (0,120))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24174131853807881"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Intensity if all lamp powers are one\n",
    "rms_uniform = rms(A*ones(n,1) .- 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip3100\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip3100)\" d=\"\n",
       "M0 1600 L2400 1600 L2400 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip3101\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip3100)\" d=\"\n",
       "M167.279 1487.47 L2352.76 1487.47 L2352.76 47.2441 L167.279 47.2441  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip3102\">\n",
       "    <rect x=\"167\" y=\"47\" width=\"2186\" height=\"1441\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  481.991,1487.47 481.991,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  871.004,1487.47 871.004,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1260.02,1487.47 1260.02,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1649.03,1487.47 1649.03,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2038.04,1487.47 2038.04,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,1487.47 2352.76,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,1247.44 2352.76,1247.44 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,1007.4 2352.76,1007.4 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,767.359 2352.76,767.359 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,527.321 2352.76,527.321 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,287.283 2352.76,287.283 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  167.279,47.2441 2352.76,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1487.47 2352.76,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1487.47 167.279,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  481.991,1487.47 481.991,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  871.004,1487.47 871.004,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1260.02,1487.47 1260.02,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1649.03,1487.47 1649.03,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2038.04,1487.47 2038.04,1465.87 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1487.47 200.061,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1247.44 200.061,1247.44 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,1007.4 200.061,1007.4 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,767.359 200.061,767.359 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,527.321 200.061,527.321 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,287.283 200.061,287.283 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip3100)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  167.279,47.2441 200.061,47.2441 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 481.991, 1541.47)\" x=\"481.991\" y=\"1541.47\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 871.004, 1541.47)\" x=\"871.004\" y=\"1541.47\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1260.02, 1541.47)\" x=\"1260.02\" y=\"1541.47\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1649.03, 1541.47)\" x=\"1649.03\" y=\"1541.47\">1.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2038.04, 1541.47)\" x=\"2038.04\" y=\"1541.47\">1.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 1504.97)\" x=\"143.279\" y=\"1504.97\">0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 1264.94)\" x=\"143.279\" y=\"1264.94\">20</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 1024.9)\" x=\"143.279\" y=\"1024.9\">40</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 784.859)\" x=\"143.279\" y=\"784.859\">60</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 544.821)\" x=\"143.279\" y=\"544.821\">80</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 304.783)\" x=\"143.279\" y=\"304.783\">100</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip3100)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 143.279, 64.7441)\" x=\"143.279\" y=\"64.7441\">120</text>\n",
       "</g>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M287.484 1475.47 L287.484 1487.47 L365.287 1487.47 L365.287 1475.47 L287.484 1475.47 L287.484 1475.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  287.484,1475.47 287.484,1487.47 365.287,1487.47 365.287,1475.47 287.484,1475.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M365.287 1439.47 L365.287 1487.47 L443.09 1487.47 L443.09 1439.47 L365.287 1439.47 L365.287 1439.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  365.287,1439.47 365.287,1487.47 443.09,1487.47 443.09,1439.47 365.287,1439.47 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M443.09 1403.46 L443.09 1487.47 L520.892 1487.47 L520.892 1403.46 L443.09 1403.46 L443.09 1403.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  443.09,1403.46 443.09,1487.47 520.892,1487.47 520.892,1403.46 443.09,1403.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M520.892 1391.46 L520.892 1487.47 L598.695 1487.47 L598.695 1391.46 L520.892 1391.46 L520.892 1391.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  520.892,1391.46 520.892,1487.47 598.695,1487.47 598.695,1391.46 520.892,1391.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M598.695 1319.45 L598.695 1487.47 L676.498 1487.47 L676.498 1319.45 L598.695 1319.45 L598.695 1319.45  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  598.695,1319.45 598.695,1487.47 676.498,1487.47 676.498,1319.45 598.695,1319.45 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M676.498 1199.43 L676.498 1487.47 L754.3 1487.47 L754.3 1199.43 L676.498 1199.43 L676.498 1199.43  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  676.498,1199.43 676.498,1487.47 754.3,1487.47 754.3,1199.43 676.498,1199.43 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M754.3 1079.41 L754.3 1487.47 L832.103 1487.47 L832.103 1079.41 L754.3 1079.41 L754.3 1079.41  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  754.3,1079.41 754.3,1487.47 832.103,1487.47 832.103,1079.41 754.3,1079.41 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M832.103 1139.42 L832.103 1487.47 L909.906 1487.47 L909.906 1139.42 L832.103 1139.42 L832.103 1139.42  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  832.103,1139.42 832.103,1487.47 909.906,1487.47 909.906,1139.42 832.103,1139.42 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M909.906 863.375 L909.906 1487.47 L987.708 1487.47 L987.708 863.375 L909.906 863.375 L909.906 863.375  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  909.906,863.375 909.906,1487.47 987.708,1487.47 987.708,863.375 909.906,863.375 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M987.708 1139.42 L987.708 1487.47 L1065.51 1487.47 L1065.51 1139.42 L987.708 1139.42 L987.708 1139.42  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  987.708,1139.42 987.708,1487.47 1065.51,1487.47 1065.51,1139.42 987.708,1139.42 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1065.51 923.384 L1065.51 1487.47 L1143.31 1487.47 L1143.31 923.384 L1065.51 923.384 L1065.51 923.384  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1065.51,923.384 1065.51,1487.47 1143.31,1487.47 1143.31,923.384 1065.51,923.384 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1143.31 1103.41 L1143.31 1487.47 L1221.12 1487.47 L1221.12 1103.41 L1143.31 1103.41 L1143.31 1103.41  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1143.31,1103.41 1143.31,1487.47 1221.12,1487.47 1221.12,1103.41 1143.31,1103.41 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1221.12 935.386 L1221.12 1487.47 L1298.92 1487.47 L1298.92 935.386 L1221.12 935.386 L1221.12 935.386  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1221.12,935.386 1221.12,1487.47 1298.92,1487.47 1298.92,935.386 1221.12,935.386 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1298.92 995.396 L1298.92 1487.47 L1376.72 1487.47 L1376.72 995.396 L1298.92 995.396 L1298.92 995.396  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1298.92,995.396 1298.92,1487.47 1376.72,1487.47 1376.72,995.396 1298.92,995.396 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1376.72 947.388 L1376.72 1487.47 L1454.52 1487.47 L1454.52 947.388 L1376.72 947.388 L1376.72 947.388  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1376.72,947.388 1376.72,1487.47 1454.52,1487.47 1454.52,947.388 1376.72,947.388 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1454.52 887.379 L1454.52 1487.47 L1532.33 1487.47 L1532.33 887.379 L1454.52 887.379 L1454.52 887.379  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1454.52,887.379 1454.52,1487.47 1532.33,1487.47 1532.33,887.379 1454.52,887.379 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1532.33 947.388 L1532.33 1487.47 L1610.13 1487.47 L1610.13 947.388 L1532.33 947.388 L1532.33 947.388  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1532.33,947.388 1532.33,1487.47 1610.13,1487.47 1610.13,947.388 1532.33,947.388 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1610.13 1091.41 L1610.13 1487.47 L1687.93 1487.47 L1687.93 1091.41 L1610.13 1091.41 L1610.13 1091.41  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1610.13,1091.41 1610.13,1487.47 1687.93,1487.47 1687.93,1091.41 1610.13,1091.41 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1687.93 1139.42 L1687.93 1487.47 L1765.73 1487.47 L1765.73 1139.42 L1687.93 1139.42 L1687.93 1139.42  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1687.93,1139.42 1687.93,1487.47 1765.73,1487.47 1765.73,1139.42 1687.93,1139.42 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1765.73 1247.44 L1765.73 1487.47 L1843.54 1487.47 L1843.54 1247.44 L1765.73 1247.44 L1765.73 1247.44  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1765.73,1247.44 1765.73,1487.47 1843.54,1487.47 1843.54,1247.44 1765.73,1247.44 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1843.54 1331.45 L1843.54 1487.47 L1921.34 1487.47 L1921.34 1331.45 L1843.54 1331.45 L1843.54 1331.45  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1843.54,1331.45 1843.54,1487.47 1921.34,1487.47 1921.34,1331.45 1843.54,1331.45 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1921.34 1391.46 L1921.34 1487.47 L1999.14 1487.47 L1999.14 1391.46 L1921.34 1391.46 L1921.34 1391.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1921.34,1391.46 1921.34,1487.47 1999.14,1487.47 1999.14,1391.46 1921.34,1391.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M1999.14 1403.46 L1999.14 1487.47 L2076.95 1487.47 L2076.95 1403.46 L1999.14 1403.46 L1999.14 1403.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1999.14,1403.46 1999.14,1487.47 2076.95,1487.47 2076.95,1403.46 1999.14,1403.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M2076.95 1415.46 L2076.95 1487.47 L2154.75 1487.47 L2154.75 1415.46 L2076.95 1415.46 L2076.95 1415.46  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2076.95,1415.46 2076.95,1487.47 2154.75,1487.47 2154.75,1415.46 2076.95,1415.46 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip3102)\" d=\"\n",
       "M2154.75 1475.47 L2154.75 1487.47 L2232.55 1487.47 L2232.55 1475.47 L2154.75 1475.47 L2154.75 1475.47  Z\n",
       "  \" fill=\"#009af9\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip3102)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2154.75,1475.47 2154.75,1487.47 2232.55,1487.47 2232.55,1475.47 2154.75,1475.47 \n",
       "  \"/>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "histogram(A*ones(n,1), bins = (0.375:0.05:1.625),\n",
    "legend = false, ylim = (0,120))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Figure 12.1** Histogram of pixel illumination values using $p = 1$ (top) and $p̂$\n",
    "(bottom). The target intensity value is one."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.1.1",
   "language": "julia",
   "name": "julia-1.1"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
